package selef.xiafan.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import selef.xiafan.dao.OrderDao;
import selef.xiafan.dao.OrderItemDao;
import selef.xiafan.dao.StockDao;
import selef.xiafan.entity.system.Stock;
import selef.xiafan.entity.order.Order;
import selef.xiafan.entity.order.OrderItem;
import selef.xiafan.service.IOrderItemService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author xiafan
 * @version 1.0
 * @date 2021/7/27 18:32
 */
@Service
public class IOrderItemServiceImpl implements IOrderItemService {
    @Autowired
    OrderItemDao orderItemDao;
    @Autowired
    StockDao stockDao;
    @Autowired
    OrderDao orderDao;

    @Override
    public void save(OrderItem orderItem) {
        String orderItemId = UUID.randomUUID().toString().substring(0, 9);
        orderItem.setOrderItemId(orderItemId);
        /*// 删除使用配件的数量
        Integer stockNum = orderItem.getStockNum();
        // 查询配件信息，进行更新
        Stock byStockId = stockD.findByStockId(orderItem.getStockId());
        byStockId.setRemainAmount(byStockId.getRemainAmount()-stockNum);
        stockD.save(byStockId);*/
        Order byOrderId = orderDao.findByOrderId(orderItem.getOrderId());
        byOrderId.setStatus(3);// 进行中
        orderDao.save(byOrderId);
        orderItemDao.save(orderItem);
    }

    @Override
    public List<OrderItem> getItemDetailByOrderId(Order order) {
        // 根据订单拿到详情
        List<OrderItem> allByOrderId = orderItemDao.findAllByOrderId(order.getOrderId());
        ArrayList<OrderItem> orderItems = new ArrayList<>();
        // 遍历详情，获取每个数据计算金额
        for (OrderItem orderItem : allByOrderId) {
            // 计算每个服务金额，服务数量
            String stockId = orderItem.getStockId();
            Stock byStockId = stockDao.findByStockId(stockId);
            // 将BigDecimal转为Int类型进行计算，数量*预售价
            BigDecimal sellPrice = byStockId.getSellPrice();

            orderItem.setPrice(sellPrice.multiply(BigDecimal.valueOf(orderItem.getStockNum())));
            // int price = sellPrice * orderItem.getStockNum();
            // orderItem.setPrice(BigDecimal.valueOf((int)price));
            orderItem.setStockName(byStockId.getName());
            orderItems.add(orderItem);
        }
        return orderItems;
    }

    @Override
    @Transactional
    public Integer deleteByOrderItem(OrderItem orderItem) {
        OrderItem byOrderItemId = null;
        if (orderItem.getOrderItemId() != null) {
            // 查询一次该信息是否真实存在
            byOrderItemId = orderItemDao.findByOrderItemId(orderItem.getOrderItemId());
            if (byOrderItemId != null) {
                orderItemDao.delete(byOrderItemId);
                return 1;// 删除成功
            }else{
                return 2;// 该信息不存在，请刷新页面或者联系管理员
            }
        }else{
            return 2;// 该信息不存在，请刷新页面或者联系管理员
        }
    }

    @Override
    public void confirmOrderItemDetail(Order order) {
        // 先查询订单明细，得到其中使用到的配件及数量
        List<OrderItem> allByOrderId = orderItemDao.findAllByOrderId(order.getOrderId());
        BigDecimal bigDecimal = new BigDecimal("0.00"); // bigDecimal进行计算时，建议使用String类型进行，降低精度损失
        for (OrderItem orderItem : allByOrderId) {
            // 从数据库查询数据对应服务数据
            Stock stockInDbByStockId = stockDao.findByStockId(orderItem.getStockId());
            stockInDbByStockId.setRemainAmount(stockInDbByStockId.getRemainAmount()-orderItem.getStockNum());
            stockDao.save(stockInDbByStockId);
            /*OrderItem byOrderItemId = orderItemDao.findByOrderItemId(orderItem.getOrderItemId());
            // 对数据库中配件数量进行修改
            byOrderItemId.setStockNum(byOrderItemId.getStockNum()-orderItem.getStockNum());
            orderItemDao.save(byOrderItemId);*/
            // 计算服务金额,根据使用的服务id查询对应信息，拿到单价后与使用数量进行相乘
            String stockId = orderItem.getStockId();
            Stock byStockId = stockDao.findByStockId(stockId);
            // 将BigDecimal转为Int类型进行计算，数量*预售价
            BigDecimal sellPrice = byStockId.getSellPrice();
            BigDecimal bigDecimal1 = new BigDecimal(sellPrice.multiply(BigDecimal.valueOf(orderItem.getStockNum())).toString());
            bigDecimal = bigDecimal1.add(bigDecimal);// 现在是结果+结果，预期是初始化+结果---解决
        }
//        System.out.println("最终结果"+bigDecimal);
        order.setTotalPrice(bigDecimal);
        order.setStatus(4);//4结束
        orderDao.save(order);
    }

    @Override
    public Integer checkItemIsNull(Order order) {
        List<OrderItem> allByOrderId = orderItemDao.findAllByOrderId(order.getOrderId());
        if(allByOrderId.size()==0)
            return 1;
        return 2;
    }
}
